package com.fuchuang.seckillsystem.controller;

import com.fuchuang.seckillsystem.dto.UserQuery;
import com.fuchuang.seckillsystem.entity.MyUserDetails;
import com.fuchuang.seckillsystem.entity.User;
import com.fuchuang.seckillsystem.service.IUserService;
import com.fuchuang.seckillsystem.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("user")
public class UserController {
    
    @Autowired
    private IUserService userService;
    
    @PostMapping("register")
    public RespBean register(@RequestBody User user) {
        userService.register(user);
        return RespBean.success();
    }
    
    @PostMapping("update")
    public RespBean update(@RequestBody User user, @AuthenticationPrincipal MyUserDetails userDetails) {
        user.setId(userDetails.getId());
        userService.update(user);
        return RespBean.success();
    }
    
    @GetMapping("userInfo")
    public RespBean getUser(@AuthenticationPrincipal MyUserDetails userDetails) {
        User user = userService.getUser(userDetails.getId());
        return RespBean.success(user);
    }
    
    @PostMapping("allUser")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_FORM')")
    public RespBean getAllUser(@RequestBody UserQuery userQuery) {
        Map<String, Object> allUser = userService.getAllUser(userQuery);
        return RespBean.success(allUser);
    }
}
